package com.mit.facade;

import java.util.List;

import org.apache.log4j.Logger;

import com.mit.common.RequestContext;
import com.mit.common.ResultContext;
import com.mit.common.exception.BaseException;
import com.mit.common.exception.DuplicateKeyException;
import com.mit.common.manufacture.dto.FactoryDTO;
import com.mit.common.purchase.dto.SupplierDTO;
import com.mit.common.security.dto.UserDTO;
import com.mit.common.token.ManufactureTokens;
import com.mit.common.token.PurchaseTokens;
import com.mit.common.token.Tokens;
import com.mit.service.core.dao.ISequenceDAO;
import com.mit.service.manufacture.dao.IFactoryDAO;

public class ManufactureFacade implements IManufactureFacade {
	
	private Logger logger = Logger.getLogger(ManufactureFacade.class);
	
	private ISequenceDAO sequenceDAO;
	private IFactoryDAO factoryDAO;

	public void setSequenceDAO(ISequenceDAO sequenceDAO) {
		this.sequenceDAO = sequenceDAO;
	}

	public void setFactoryDAO(IFactoryDAO factoryDAO) {
		this.factoryDAO = factoryDAO;
	}

	
	@Override
	public ResultContext retrievePagedFactorys(RequestContext rqs) {
		UserDTO user = rqs.getUser();
		String key = (String) rqs.getParameter(ManufactureTokens.KEY_FACTORY_NAME);
		Integer pageNo = (Integer) rqs.getParameter(Tokens.KEY_PAGE_NO);
		Integer pageSize = (Integer) rqs.getParameter(Tokens.KEY_PAGE_SIZE);
		List<FactoryDTO> lst = factoryDAO.retrievePagedFactorys(user.getOrgId(), key, pageNo, pageSize);
		ResultContext rlt = new ResultContext();
		rlt.addResult(ManufactureTokens.KEY_FACTORY_LIST, lst);
		return rlt;
	}

	@Override
	public ResultContext retrieveFactoryCount(RequestContext rqs) {
		UserDTO user = rqs.getUser();
		String key = (String) rqs.getParameter(ManufactureTokens.KEY_FACTORY_NAME);
		Integer rowCount = factoryDAO.retrieveFactorysCount(user.getOrgId(),key);
		ResultContext rlt = new ResultContext();
		rlt.addResult(Tokens.KEY_RECORD_COUNT, rowCount);
		return rlt;
	}

	@Override
	public ResultContext saveFactory(RequestContext rqs) throws BaseException {
		FactoryDTO factory = (FactoryDTO) rqs.getParameter(ManufactureTokens.KEY_FACTORY_DTO);
		UserDTO user = rqs.getUser();
		factory.setOrgId(user.getOrgId());
		
		if(factory.getId() < 1){
			logger.debug("new Factory");
			//验证：简称不能重复
			if(factoryDAO.hasFactory(user.getOrgId(), null, factory.getName())){
				logger.error("Duplicate Name: " + factory.getName());
				throw new DuplicateKeyException(factory.getName());
			}
			factory = factoryDAO.createFactoryDTO(factory);
			logger.info("new Factory created: " + factory.getName());
		}else{
			logger.debug("update Factory");
			//验证：简称不能重复
			if(factoryDAO.hasFactory(user.getOrgId(),factory.getId(), factory.getName())){
				logger.error("Duplicate Name: " + factory.getName());
				throw new DuplicateKeyException(factory.getName());
			}
			factoryDAO.updateFactoryDTO(factory);
			logger.info("Factory updated: " + factory.getName());
		}
		ResultContext rlt = new ResultContext();
		rlt.addResult(ManufactureTokens.KEY_FACTORY_DTO, factory);
		return rlt;
	}

	@Override
	public ResultContext retrieveFactoryBasicInfo(RequestContext rqs) {
		UserDTO user = rqs.getUser();
		String name = (String) rqs.getParameter(ManufactureTokens.KEY_FACTORY_NAME);
		List<FactoryDTO> lst = factoryDAO.retrieveFactoryBasicInfo(user.getOrgId(), name);
		ResultContext rlt = new ResultContext();
		rlt.addResult(ManufactureTokens.KEY_FACTORY_LIST, lst);
		return rlt;
	}




}
